package com.yvtian.om.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.dao.InvBomMapper;
import com.yvtian.inv.dao.InvBomShopMapper;
import com.yvtian.inv.pojo.InvBomShopEntity;
import com.yvtian.inv.service.InvBomService;
import com.yvtian.inv.service.InvBomShopService;
import com.yvtian.om.pojo.OmMrpEntity;
import com.yvtian.om.dao.OmMrpMapper;
import com.yvtian.om.pojo.OmMrpItemEntity;
import com.yvtian.om.service.OmMrpItemService;
import com.yvtian.om.service.OmMrpService;
import com.yvtian.pu.dao.PuApplyForShopMapper;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * mrp运算(om_mrp)表服务实现类
 *
 * @author long
 * @since 2024-07-05 09:58:32
 */
@Service("omMrpService")
public class OmMrpServiceImpl extends ServiceImpl<OmMrpMapper, OmMrpEntity> implements OmMrpService {

    @Autowired
    private OmMrpItemService omMrpItemService;
    @Autowired
    private InvBomService invBomService;
    @Autowired
    private InvBomShopService invBomShopService;
    @Autowired
    private InvBomShopMapper invBomShopMapper;
    @Autowired
    private PuApplyForShopMapper puApplyForShopMapper;

    @Override
    public IPage<OmMrpEntity> findByPage(Page<OmMrpEntity> page, OmMrpEntity omMrpEntity) {
        LambdaQueryWrapper<OmMrpEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmMrpEntity::getIsDel, 0);
        getQueryWrapper(omMrpEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    private void getQueryWrapper(OmMrpEntity omMrpEntity, LambdaQueryWrapper<OmMrpEntity> queryWrapper) {
        queryWrapper
                .like(StringHelper.isNotEmpty(omMrpEntity.getCode()), OmMrpEntity::getCode, omMrpEntity.getCode())
                .like(StringHelper.isNotEmpty(omMrpEntity.getName()), OmMrpEntity::getName, omMrpEntity.getName())
                .like(StringHelper.isNotEmpty(omMrpEntity.getBomCode()), OmMrpEntity::getBomCode, omMrpEntity.getBomCode())
                .like(StringHelper.isNotEmpty(omMrpEntity.getBomName()), OmMrpEntity::getBomName, omMrpEntity.getBomName())
                .eq(omMrpEntity.getMrpQuantity() != null, OmMrpEntity::getMrpQuantity, omMrpEntity.getMrpQuantity())
                .like(StringHelper.isNotEmpty(omMrpEntity.getOmOrderCode()), OmMrpEntity::getOmOrderCode, omMrpEntity.getOmOrderCode())
                .like(StringHelper.isNotEmpty(omMrpEntity.getShopCode()), OmMrpEntity::getShopCode, omMrpEntity.getShopCode())
                .like(StringHelper.isNotEmpty(omMrpEntity.getShopName()), OmMrpEntity::getShopName, omMrpEntity.getShopName())
                .eq(omMrpEntity.getStatus() != null, OmMrpEntity::getStatus, omMrpEntity.getStatus())
                .like(StringHelper.isNotEmpty(omMrpEntity.getDelName()), OmMrpEntity::getDelName, omMrpEntity.getDelName())
                .like(StringHelper.isNotEmpty(omMrpEntity.getCreateName()), OmMrpEntity::getCreateName, omMrpEntity.getCreateName())
                .like(StringHelper.isNotEmpty(omMrpEntity.getCreateUser()), OmMrpEntity::getCreateUser, omMrpEntity.getCreateUser())
                .like(StringHelper.isNotEmpty(omMrpEntity.getUpdateName()), OmMrpEntity::getUpdateName, omMrpEntity.getUpdateName())
                .like(StringHelper.isNotEmpty(omMrpEntity.getUpdateUser()), OmMrpEntity::getUpdateUser, omMrpEntity.getUpdateUser())
                .like(StringHelper.isNotEmpty(omMrpEntity.getRemarks()), OmMrpEntity::getRemarks, omMrpEntity.getRemarks())
                .like(StringHelper.isNotEmpty(omMrpEntity.getCheckName()), OmMrpEntity::getCheckName, omMrpEntity.getCheckName())
                .like(StringHelper.isNotEmpty(omMrpEntity.getCheckUser()), OmMrpEntity::getCheckUser, omMrpEntity.getCheckUser());
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(OmMrpEntity omMrpEntity) {
        ArrayList<OmMrpItemEntity> omMrpItemList = omMrpEntity.getOmMrpItemList();
        if (omMrpItemList != null && !omMrpItemList.isEmpty()) {
            LambdaUpdateWrapper<OmMrpItemEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(OmMrpItemEntity::getZcode, omMrpEntity.getCode());
            omMrpItemService.remove(updateWrapper);
            omMrpItemService.saveBatch(omMrpItemList);
        }
        this.saveOrUpdate(omMrpEntity);
        return true;
    }

    @Override
    public OmMrpEntity computeMrp(OmMrpEntity omMrpEntity) {
        ArrayList<OmMrpItemEntity> omMrpItemList = new ArrayList<>();
        Integer mrpQuantity = omMrpEntity.getMrpQuantity();//数量
        String bomCode = omMrpEntity.getBomCode();
        //计算需求数量和库存
        List<Map<String, Object>> quantityList = invBomShopMapper.selectShopQuantityList(bomCode);
        ArrayList<Map<String, Object>> quantityReceivedList = puApplyForShopMapper.selectShopQuantityList(bomCode);
        Map<String, Integer> quantityReceivedMap = quantityReceivedList.stream()
                .collect(Collectors.toMap(
                        map -> (String) map.get("shopCode"),
                        map -> (Integer) map.get("inTransitQuantity")
                ));
        for (Map<String, Object> objectMap : quantityList) {
            OmMrpItemEntity omMrpItemEntity = new OmMrpItemEntity();
            String shopCode = String.valueOf(objectMap.get("shopCode"));
            omMrpItemEntity.setZcode(omMrpEntity.getCode());
            omMrpItemEntity.setShopCode(shopCode);
            omMrpItemEntity.setShopName(String.valueOf(objectMap.get("shopName")));
            //库存
            omMrpItemEntity.setQuantity(Integer.parseInt(String.valueOf(objectMap.get("quantity"))));
            //需求数
            omMrpItemEntity.setRequiredQuantity(Integer.parseInt(String.valueOf(objectMap.get("requiredCount"))) * mrpQuantity);
            //在途数
            Integer inTransitQuantity = quantityReceivedMap.get(shopCode);
            omMrpItemEntity.setInTransitQuantity(inTransitQuantity);
            //不足数
            int unEnough = omMrpItemEntity.getQuantity() - omMrpItemEntity.getRequiredQuantity() - inTransitQuantity;
            omMrpItemEntity.setUnEnough(unEnough);
        }

        //TODO 计算不足数量，需考虑使用MRP计算采购和生产计划两种单据的物料
        return omMrpEntity;
    }
}

